package com.ktjy.controller;

import com.ktjy.pojo.SongList;
import com.ktjy.service.ISongListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
public class SongListController {

    @Autowired
    ISongListService songListService;

    /**
     * 查询分页数据
     *
     * @return
     */

    @RequestMapping("/pgeList")
    public Object getPageInfo(Model model, @RequestParam(value = "startTime", defaultValue = "") String startTime,
                              @RequestParam(value = "endTime", defaultValue = "") String endTime,
                              @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        Map result = new HashMap();
        int totalCount = songListService.getCount(startTime, endTime);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("startTime", startTime);
        model.addAttribute("endTime", endTime);
        model.addAttribute("totalPage", totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("dataList", songListService.getPageList(startTime, endTime, pageNo, pageSize));
        return "list";
    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("/goAdd")
    public Object goAdd() {
        return "add";
    }

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping("/save")
    public Object insertSongList(SongList songList) {
        int rowCount = songListService.insertSongList(songList);

        if (rowCount > 0) {
            return "redirect:pgeList";
        } else {
            return "redirect:goAdd";
        }
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping("/del")
    public Object delSongList(@RequestParam(value = "id", defaultValue = "0") Long id) {
        int rowCount = songListService.delSongList(id);

        return "redirect:pgeList";

    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("/goEdit")
    public Object goEdit(Model model, @RequestParam(value = "id", defaultValue = "0") Long id) {
        model.addAttribute("songList", songListService.getById(id));
        return "edit";
    }

    /**
     * 修改
     *
     * @return
     */
    @RequestMapping("/update")
    public Object updateSongList(SongList songList) {
        int rowCount = songListService.updateSongList(songList);

        if (rowCount > 0) {
            return "redirect:pgeList";
        } else {
            return "redirect:goEdit";
        }
    }
}
